package com.leyou.item.controller;

import com.leyou.item.dto.BrandDTO;
import com.leyou.common.vo.PageResult;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.item.controller
 * @date 2019/6/24 20:55
 * @description:
 **/
@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌页面
     * @param page
     * @param rows
     * @param sortBy
     * @param key
     * @param desc
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required =false ) String sortBy,
            @RequestParam(value = "key",required =false) String key,
            @RequestParam(value = "desc",defaultValue = "false") Boolean desc

    ){

        return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,sortBy,key,desc));
    }


    /**
     * 新增品牌
     * @param dto
     * @param ids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO dto,@RequestParam("cids") List<Long> ids){
        brandService.saveBrand(dto,ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新品牌
     * @param dto
     * @param ids
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO dto,@RequestParam("cids") List<Long> ids){
        brandService.updateBrand(dto,ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据cid回显品牌信息
     * @param cid
     * @return
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryByCid(@RequestParam("id") Long cid){
        return ResponseEntity.ok(brandService.queryByCid(cid));
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id){
        BrandDTO brand = brandService.queryById(id);
        return ResponseEntity.ok(brand);
    }

    /**
     * 根据品牌id批量查询品牌
     * @param idList 品牌id的集合
     * @return 品牌的集合
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> idList){
        return ResponseEntity.ok(brandService.queryBrandByIds(idList));
    }
}
